/** 
*
* @author  jiwf 
* @date 创建时间：2016年11月19日 下午10:21:00 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.impl.client.RoutedRequest;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xbstar.bean.AffixInfo;
import com.xbstar.bean.ConfigInfo;
import com.xbstar.bean.DestinationInfor;
import com.xbstar.bean.UserInfo;
import com.xbstar.bean.UserOrder;
import com.xbstar.dao.AffixInfoRepository;
import com.xbstar.dao.ConfigInfoRepository;
import com.xbstar.dao.DestinationRepository;
import com.xbstar.dao.OrderRepository;
import com.xbstar.dao.RouteInfoRepository;
import com.xbstar.dao.UserInfoRepository;
import com.xbstar.tools.BeanUtils;



@RestController
@RequestMapping("/config")
public class ConfigController {
	@Autowired
	ConfigInfoRepository configRe;
	@Autowired
	AffixInfoRepository affixRe;
	@Autowired
	UserInfoRepository userRe;
	@Autowired
	RouteInfoRepository routeRe;
	@Autowired
	DestinationRepository destRe;
	@Autowired
	OrderRepository orderRe;
	@RequestMapping("/get")
	public String getConfig(){
		JSONObject json=new JSONObject();
		JSONObject tjson=new JSONObject();
		List<ConfigInfo>ree=(List<ConfigInfo>) configRe.findAll();
		if(ree!=null&&ree.size()>0){
			ConfigInfo config=ree.get(0);
			 tjson=BeanUtils.getAJsonFromObj(config,null);
			List<AffixInfo>affixArr= affixRe.findALLByBelongTo(config.getAffixBelongTo());
			if(affixArr!=null&&affixArr.size()>0){
				tjson.put("imgSrc", affixArr.get(0).getUrl());
			}
			json.put("data", tjson);
		}
		json.put("status", "200");
		return json.toString();
	}
	@RequestMapping("/set")
	public String setConfig(HttpServletRequest req){
		JSONObject json=new JSONObject();
		JSONObject tjson=new JSONObject();
		ConfigInfo config=new ConfigInfo();
		try {
			config=BeanUtils.copyRequestToObj(config, req);
			config.setId((long) 1);
			configRe.save(config);
			json.put("status", "200");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json.toString();
	}
	@RequestMapping("/total")
	public String getHomeTotal(){
		JSONObject json=new JSONObject();
		JSONObject tjson=new JSONObject();
		 Long userC=userRe.count();
		tjson.put("userTotal", userC);
		Long destC=destRe.count();
		tjson.put("desTotal", destC);
		Long orderc=orderRe.count();
		tjson.put("orderTotal", orderc);
		Long routeC=routeRe.count();
		tjson.put("routeTotal", routeC);
		json.put("status", "200");
		json.put("data", tjson);
		return json.toString();
	}
	/**
	 * $scope.ele={
	  desTotal:14,
	  routeTotal:18,
	  leaderTotal:20,
	  userTotal:40
  };
	 */
}
